/*
 * win_func.c
 *
 *  Created on: 2017��2��21��
 *      Author: Anmali
 */

#include <stdint.h>
#include "math.h"
#include "win_func.h"
#include "math_statistics_func.h"
#include "arm_cos_f32.h"
#include "calc_type.h"

/* ----------------------------------------------------------------------
** the rectangular window function
** flag:0:bypass  other:rectangular window
** len:the length of input data array x_in
** win_width:the width of rectangular window
** begin_loc:the begin loc of rectangular window
** x_in:input data array
** x_out:output data array
** ------------------------------------------------------------------- */
void rectangular(uint8_t flag, uint32_t len, uint32_t win_width, uint32_t begin_loc,
						float* x_in, float* x_out)
{
    if (flag != 0) {
		arm_fill_f32(0.0, x_out, begin_loc);
		arm_copy_f32(&x_in[begin_loc], &x_out[begin_loc], win_width);
		arm_fill_f32(0.0, &x_out[begin_loc + win_width], len - (begin_loc + win_width));
    } else {
		arm_copy_f32(x_in + begin_loc, x_out, len);
    }
}
/* ----------------------------------------------------------------------
** the hanning window function
** flag:0:bypass  other:hanning window
** len:the length of input data array x_in
** x_in:input data array
** x_out:output data array
** ------------------------------------------------------------------- */
void hanning(uint8_t flag, uint32_t len, float *x_in, float *x_out)
{
	int i;
	float a0,a1;

	//the parameter of hanning window
	a0 = 0.5;
	a1 = 0.5;

    if (flag == 1) {
	    for(i = 0; i < len; i++)
	    	//x_out[i] = x_in[i] * (a0 - (a1 * cos((2 * L_PI * i) / len)));
	    	x_out[i] = x_in[i] * (a0 - (a1 * arm_cos_f32((2 * L_PI * i) / (len - 1))));
    } else if (flag == 2){
    	for(i = 0; i < len; i++)
	    	//x_out[i] = x_in[i] * (1 - cos((2 * L_PI * i) / len));			//幅值相等恢复系数2
	    	x_out[i] = x_in[i] * (1 - arm_cos_f32((2 * L_PI * i) / (len - 1)));
    } else if (flag == 3) {
        for(i = 0; i < len; i++)
	    	//x_out[i] = x_in[i] * (1 - cos((2 * L_PI * i) / len)) * 0.8165;//功率相等恢复系数1.633
	    	x_out[i] = x_in[i] * (1 - arm_cos_f32((2 * L_PI * i) / (len - 1))) * 0.8165;
    } else {
    	arm_copy_f32(x_in, x_out, len);
    }
}
/* ----------------------------------------------------------------------
** the hamming window function
** flag:0:bypass  other:hanning window
** len:the length of input data array x_in
** x_in:input data array
** x_out:output data array
** ------------------------------------------------------------------- */

void hamming(uint8_t flag, uint32_t len, float* x_in, float* x_out)
{
    int i;
	float a0,a1;
	
	//the parameter of hamming window
	a0 = 0.54;
	a1 = 0.46;

    if (flag == 1) {
        for (i = 0; i < len; i++)
            //x_out[i] = x_in[i] * (0.54 - 0.46 * cos((2 * L_PI * i) / (len - 1)));
			x_out[i] = x_in[i] * (a0 - a1 * arm_cos_f32((2 * L_PI * i) / (len - 1)));
    } else if (flag == 2) {
        for (i = 0; i < len; i++)					//幅值相等恢复系数1.852
            //x_out[i] = x_in[i] * (0.54 - 0.46 * cos((2 * L_PI * i) / (len - 1))) * 1.852;
			x_out[i] = x_in[i] * (a0 - a1 * arm_cos_f32((2 * L_PI * i) / (len - 1))) * 1.852;
    } else if (flag == 3) {
		for (i = 0; i < len; i++)					//功率相等恢复系数1.586
            //x_out[i] = x_in[i] * (0.54 - 0.46 * cos((2 * L_PI * i) / (len - 1))) * 1.586;
			x_out[i] = x_in[i] * (a0 - a1 * arm_cos_f32((2 * L_PI * i) / (len - 1))) * 1.586;
    } else {
		arm_copy_f32(x_in, x_out, len);
    }
}
